package cn.fuxi2022.accounts.register.listener;

import cn.fuxi2022.accounts.register.service.GroupRegisterService;
import cn.fuxi2022.rabbitmq.appletsPay.constants.RegisterConstants;
import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Slf4j
@Component
public class RegisterConsumer {

    @Autowired
    private GroupRegisterService groupRegisterService;

    /**
     * 处理签到队列 监听接口
     */
    @RabbitListener(queues = RegisterConstants.GROUP_REGISTER_TTL_REGISTER_QUEUE)
    public void registerListener(String data, Channel channel, Message message) throws IOException {
        // 解析JSON
        Map<String, String> map = JSON.parseObject(data, Map.class);
        log.info(String.valueOf(map));
        // 获取用户要签到的活动ID
        String activityId = map.get("EventKey");
        // 获取用户openId
        String openId = map.get("FromUserName");

        groupRegisterService.studentRegister(activityId, openId);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
